package demo1;

import demo1.entity.Product;
import demo1.entity.User;
import demo1.service.AccountService;
import demo1.service.ProductService;
import demo1.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;
import java.util.List;

public class TestApp {
    UserService us;
    AccountService as;
    ProductService ps;

    @Before
    public void init(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("demo1/demo1.xml");
        us=ac.getBean(UserService.class);
        as=ac.getBean(AccountService.class);
        ps=ac.getBean(ProductService.class);
    }

    @Test
    public void userTest(){
        //System.out.println(us.login("a1","123"));
        us.regitst(new User(2,"a2","123"));
    }

    @Test
    public void accountTest(){
        try{
            as.transfer("a1","a2",100.0);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            as.query("a1");
            as.query("a2");
        }
    }

    @Test
    public void accountSelect(){
        as.query("a1");
    }

    @Test
    public void productTest(){
        try {
            List list = Arrays.asList(new Product(1, "phon1", 1000d), new Product(2, "phon2", 1300d));
            ps.addProducts(list);
        }finally {
            ps.queryProduct("phon1");
        }
    }
}
